/**
 * The Free Gaming System; callbacks definition and manipulation
 * functions.
 *
 * Copyright 2006 the Free Gaming System developers.
 *
 * FGS is the legal property of its developers, whose names are too
 * numerous to list here.  Please refer to the COPYRIGHT file
 * distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "fgs_game.h"
#include "fgs_internal.h"

/*
 * Callbacks and helper functions for the TCP networking layer
 */

fgs_error_t
fgs__callbacks_tcp_recv(fgs_baton_t b, char *s, int len, fgs_bool_t dequeue)
{
  int fd = (int)b;
  int ret, len_read = 0;
  int flags = (dequeue == FGS_FALSE ? MSG_PEEK: 0);

  while (len_read < len)
    {
      ret = recv(fd, &s[len_read], len-len_read, flags);
      if (ret == -1)
        return FGS_ERR_SOCKET_ERROR;
      if (ret == 0)
        return FGS_ERR_SOCKET_CLOSED;

      len_read += ret;
    }
  return FGS_OK;
}

fgs_error_t
fgs__callbacks_tcp_send(fgs_baton_t b, char *data, int len)
{
  int fd = (int)b;
  int len_sent, len_left = len;

  while (len_left != 0)
    {
      len_sent = send(fd, &data[len-len_left], len_left, 0);
      if (len_sent == -1)
        return FGS_ERR_SOCKET_ERROR;
      len_left -= len_sent;
    }

  return FGS_OK;
}

static fgs_bool_t
tcp_cmp(fgs_baton_t f1, fgs_baton_t f2)
{
  return (int)f1 == (int)f2 ? FGS_TRUE : FGS_FALSE;
}

static void
init_callbacks_tcp(fgs_callbacks_t *cb_table)
{
  cb_table->read_client = fgs__callbacks_tcp_recv;
  cb_table->write_client = fgs__callbacks_tcp_send;
  cb_table->cmp_client = tcp_cmp;
}


/*
 * General callback management API.
 */
fgs_error_t
fgs_init_callbacks(fgs_callbacks_t **cb_table,
                   enum fgs_net_type init_type)
{
  *cb_table = fgs__alloc(1, sizeof(**cb_table));
  if (*cb_table == NULL)
    return FGS_ERR_INTERNAL;

  switch (init_type)
    {
    case FGS_NONE:
      break;
    case FGS_TCP:
      init_callbacks_tcp(*cb_table);
      break;
    default:
      fgs__free(*cb_table);
      return FGS_ERR_INVALID_NET_TYPE;
    }

  return FGS_OK;
}

fgs_error_t
fgs__callbacks_validate(fgs_callbacks_t *cb_table)
{
  if (cb_table->read_client == NULL ||
      cb_table->write_client == NULL ||
      cb_table->cmp_client == NULL)
    return FGS_ERR_INVALID_CALLBACKS;
  else
    return FGS_OK;
}
